/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-10-21
 * Time: 22:18
 */
class Solution {
    public void swap(int [] arr,int index1,int index2){
        int temp=arr[index1];
        arr[index1]=arr[index2];
        arr[index2]=temp;
    }
    public void sortColors(int[] nums) {
        int index1=0;
        int index2=0;
        int length=nums.length;
        for(int i=0;i<length;i++){
            int temp=nums[i];
            if(temp==0){
                swap(nums,i,index1);
                //如果把1换到了2的后面
                if(index1<index2){
                    swap(nums,i,index2);
                }
                index1++;
                index2++;
            }else if(temp==1){
                swap(nums,i,index2);
                index2++;
            }
        }
    }
}
